package com.example.demo.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

//此类用来表示在线状态的人数
@Component
public class GetUserLoginMatch {
    //保证线程安全
    private ConcurrentHashMap<Integer,WebSocketSession> mapTatch =new ConcurrentHashMap<>();//匹配模块的在线状态
    private ConcurrentHashMap<Integer,WebSocketSession> mapGame =new ConcurrentHashMap<>();//对战模块的在线状态
    /**
     * 上线时在线状态加入map中
     */
    public void enterGameHall(int userID,WebSocketSession session) {
        mapTatch.put(userID,session);
    }//匹配
    public void enterGameGame(int userID,WebSocketSession session) {
        mapGame.put(userID,session);
    }//对战
    /**
     * 下线时退出map的登录状态
     */
    public void exitGameHall(int userID) {
        mapTatch.remove(userID);
    }//匹配
    public void exitGameGame(int userID) {
        mapGame.remove(userID);
    }//对战
    /**
     * 返回当前在线用户的session信息
     * @param userID
     * @return
     */
    public WebSocketSession getFromGameHall(int userID) {
        return mapTatch.get(userID);
    }//匹配
    public WebSocketSession getFromGameGame(int userID) {
        return mapGame.get(userID);
    }//对战
}
